<template>
  <div class="wrap wrapContent">
    <div style="width: 100%; height: 100vh;overflow:auto">
      <ul id="zTreeId" class="ztree" style="margin-left:30px" :class="zNodes.length==1?'single':''"></ul>
    </div>
  </div>
</template>
<script>
import { levelList,findZzjg,fetchList2,getAllMonth } from '@/api/user'
import {getMonthPv2} from '@/api/pv'
// 用户添加编辑页面
export default {
  data() {
    return {
      userLevel1:  ['普通会员', '经销商', '副总监', '总监', '区域总监','高级区域总监', '亚洲总监', '亚太区总监', '环球总监'],
      setting: {
        view: {
          dblClickExpand: false
        },
        data: {
          simpleData: {
            enable: true,
            idKey: "id",
            pIdKey: "pid",
            rootPId: this.$route.query.userId || 0,
          },
        },
      },
      zNodes: [],
      // 数据
      zTreeObj: null
    }
  },
  // 页面初始化参数加载
  mounted() {
    this.getLevelList()
    // 页面初始化参数设置
  },
  methods: {
    onDblClick(e){
      return false
    
    },
    contains2(a, obj,key) {
        var i = a.length;
        while (i--) {
            if (a[i][key] == obj[key]) {

                return i;
            }
        }
        return false;
    },

    async getLevelList() {

            let response3=await getAllMonth({})
            if(this.$route.query.type=='all'){
              
              let response=await findZzjg({})
                this.zNodes = response.data.data
          
                let data={
                  id: 0,
                  pid: -1,
                  post: 2,
                  pvTotal: '',
                  name: "幸福滋味（公司）",
                  userLevel: '',
                  username: "",
                }

                this.zNodes.unshift(data)
                for (let index = 0; index < this.zNodes.length; index++) {
                  const element = this.zNodes[index]
                  element['monthPv']=0
                  for (let i = 0; i < response3.data.length; i++) {
                    const element2 = response3.data[i];
                    if(element.id == element2.user_id){
                      element['monthPv']+=Number(element2.integral_price)
                    }
                  }
                  if(element.id!=0){
                      element['name'] = element.surname + element.username + ' ' + ' ' + ' ' + this.userLevel1[element.post - 1] + ' 銷售PV  ' + (element.monthPv || 0)
                    }
                  if(element.pid==null){
                    element.pid = 0
                  }
                  element.open=true
                }

                this.zTreeObj = $.fn.zTree.init($('#zTreeId'), this.setting, this.zNodes)
          
              return
            }
            let all=await findZzjg({})
            all = all.data.data
            levelList({ userId: this.$route.query.userId }).then(async (response) => {

              if (response.data.errno == 0) {
                this.zNodes = response.data.data
                for (let index = 0; index < this.zNodes.length; index++) {
                  const element = this.zNodes[index]
                  element['monthPv']=0
                  for (let i = 0; i < response3.data.length; i++) {
                    const element2 = response3.data[i];
                    if(element.id == element2.user_id){
                      element['monthPv']+=Number(element2.integral_price)
                    }
                  }
                    for (let i = 0; i < all.length; i++) {
                        const allitem = all[i];
                        if(this.contains2(this.zNodes,allitem,'id')==false && allitem.pid==element.id  ){
                            this.zNodes.push(allitem)
                        }
                    }
                    
                  element['name'] = element.surname + element.username + ' ' + ' ' + ' ' + this.userLevel1[element.post - 1] + ' 銷售PV  ' + (element.monthPv || 0)
                  element.open=true
                }
                this.zTreeObj = $.fn.zTree.init($('#zTreeId'), this.setting, this.zNodes)
              } 
            })
       
          
    }
  }
}
</script >

<style >
.wrapContent {
  width: 80%;
  margin: 40px auto;
}
.ztree .ico_docu{display: none!important;}
.ztree .ico_open{ display: none!important;}
.ztree .ico_close{ display: none!important;}
</style>
